<?php
class BaseAction extends Action {
	public $adminId;

	public function _initialize() {

		if ( !service('Passport')->isLoggedAdmin() ) {
			$this->redirect( 'Admin://Login/index' );
		}
		
		//用于控制，外部访问时验证临时的登陆码。
		if(C('ADMIN_SEND_LOGIN_EMAIL')){
			if(empty($_SESSION['ADMIN_VERIFYCODE'])){
				$this->activate($_SESSION['adminEmail']);
				exit;
			}else{
				if($_SESSION['ADMIN_VERIFYCODE']=='verified'){
					
				}else{
					if($_SESSION['ADMIN_VERIFYCODE']!=$_REQUEST['validationcode']){
						$this->activate($_SESSION['adminEmail']);
						exit;
					}else{
						$_SESSION['ADMIN_VERIFYCODE']='verified';
					}
				}
			}
		}
		
		$_REQUEST['goPageFun'] = "adminSearchGoPage";//默认的js分页函数
		$this->adminId = $_SESSION['adminId'];
	}
	
	/**条件搜索,将搜索条件变量加入到模版中.
	 **/
	protected function assignRequestParams(){
		unset($_REQUEST['__hash__']);
		unset($_REQUEST['pno']);
		unset($_REQUEST['goPageFun']);
		$this->assign($_REQUEST);
	}
	
	protected function _getSearchMap($fields) {
		// 为使搜索条件在分页时也有效，将搜索条件记录到SESSION中
		if ( !empty($_POST) ) {
			$_SESSION['admin_search_attach'] = serialize($_POST);
		}else if ( isset($_GET[C('VAR_PAGE')]) ) {
			$_POST = unserialize($_SESSION['admin_search_attach']);
		}else {
			unset($_SESSION['admin_search_attach']);
		}
		
		// 组装查询条件
		$map	= array();
		foreach ($fields as $k => $v) {
			foreach ($v as $field) {
				if ( isset($_POST[$field]) && $_POST[$field] != '' ) {
					if($k == 'in') {
						$map[$field] = array($k, explode(',', $_POST[$field]));
					}else {
						$map[$field] = array($k, $_POST[$field]);					
					}
				}
			}
		}
		
		return $map;
	}
	

//发送激活邮件，为了保证确保非法登陆。
	public function activate($email) {
		$ADMIN_VERIFYCODE=md5($email.rand(10000000,99999999));
		//设置激活路径
		$activate_url	 = SITE_URL.U( 'Admin://Index/index',array( 'validationcode'=>$ADMIN_VERIFYCODE ) );//
		$_SESSION['ADMIN_VERIFYCODE']=$ADMIN_VERIFYCODE;
		//设置邮件模板
		$body = <<<EOD
管理员登陆激活链接!<br>

请马上点击进入管理员界面！<br>

<a href="$activate_url" target='_blank'>$activate_url</a><br/>

如果通过点击以上链接无法访问，请将该网址复制并粘贴至新的浏览器窗口中。<br/>

EOD;
		// 发送邮件
		global $ts;
		$email_sent = service('Mail')->send_email($email, "{$ts['site']['site_name']}管理员登陆激活链接",$body);
		echo "无权访问！";
		exit;
	}
	
}